// t6Adept.txt
// Modified basicnpc.txt, used for the Archer Adept in the Waterfall Cave.
// Has a dialog box that pops up when party first sees it.
// Memory Cells:
//   Cell 0 - How creature moves.
//     0 - If 0, wander randomly. 
//     1 - Stands still until a target appears.
//     2 - Completely immobile, even if target appears.
//   Cell 1,2 - Stuff done flag. When the Adept is killed, this flag is
//     set to equal 3.
//   Cell 3 - Dialogue node to start with if talked to. if left at 0, this
//     character doesn't talk.
//   Cell 4,5 - SDF, makes sure that party only gets that intro dialog
//     once. (Set to one once party first sees the Adept)

begincreaturescript;

variables;

short i,target;
short arrow_nock = 0;
short atk_ok = 0;
short trgt = 0;
short count = 0;

body;

beginstate INIT_STATE;
	if (get_memory_cell(0) == 2)
		set_mobility(ME,0);
	set_aggression(ME,100);
	set_courage(ME,50);
	set_strategy(ME,1);

	break;

beginstate DEAD_STATE;
	// Set the appropriate stuff done flag for this character being dead
	reset_dialog();
	add_dialog_str(0,"The archer falls to the ground with a confused look on his face. A few arrows linger in the air for a while until gravity catches up with them, and they clatter harmlessly to the ground.",0);
	add_dialog_str(1,"Bending over to examine one of the arrows, you notice that it's still glowing faintly. Maybe some of them can be put to a good use again, namely yours.",0);
	run_dialog(1);

	if ((get_memory_cell(1) != 0) || (get_memory_cell(2) != 0))
		set_flag(get_memory_cell(1),get_memory_cell(2),3);
break;

beginstate START_STATE; 
	//Intro text
	if((party_can_see_loc(my_loc_x(),my_loc_y()) == 1) && (get_flag(get_memory_cell(4),get_memory_cell(5)) == 0)) {
		reset_dialog();
		add_dialog_str(0,"Another rebel archer comes into view, but this one looks a bit different. Then, you realize that it's the faint blue glow coming from the tips of his arrows.",0);
		add_dialog_str(1,"The effect is eerie to say the least, and it makes the quiver strapped to his back look like a torch.",0);
		add_dialog_str(2,"He doesn't say anything, he just draws a handful of arrows... and leaves them floating in the air beside him while he notches the first one.",0);
		run_dialog(1);
		set_flag(get_memory_cell(4),get_memory_cell(5),1);
		}

	if((char_ok(trgt) == 0) || (char_dist_to_loc(trgt,char_loc_x(my_number()),char_loc_y(my_number())) < 3) || (can_see_char(trgt) == 0))
		atk_ok = 0;
	count = 0;
	while((count < 10) && (atk_ok == 0)) {
		trgt = random_party_member();
		if((char_ok(trgt) == 1) && (char_dist_to_loc(trgt,char_loc_x(my_number()),char_loc_y(my_number())) >= 3) && (can_see_char(trgt) == 1))
			atk_ok = 1;
		count = (count + 1);
	}
	if(atk_ok == 1)  {
		//THE ATTACK GOES HERE!!!
		put_straight_zap(my_loc_x(), my_loc_y(), char_loc_x(trgt), char_loc_y(trgt), 6);
		put_sparkles_on_char(trgt, 6, 1);
		run_animation_sound(91);
		
		damage_char(trgt, ((get_max_health(trgt) * 4) / 5),  5); //four-fifths of max health, cold damage

		if(get_flag(6,7) < 2) {
			message_dialog("The archer's arrow leaves a streak of blue light behind it, and you feel a deep, cold ache in your bones.","");
		set_flag(6,7,2);
		}
		else
			print_str("The archer fires a magic arrow!");

		end_combat_turn();

	}
	else
		do_attack();
	

	// if I have a target for some reason, go attack it
	 if (target_ok()) {
		if (dist_to_char(get_target()) <= 16)
			else 
				set_target(ME,-1);
		}
	
	// Look for a target, attack it if visible
	 if (select_target(ME,8,0)) {
		do_attack();
		}
		
	// Have I been hit? Strike back!
	if (who_hit_me() >= 0) {
		set_target(ME,who_hit_me());
		do_attack();
		}
		
	// Otherwise, just peacefully move around. Go back to start, if I'm too far
	// from where I started.
	if ((my_dist_from_start() >= 6) || ((my_dist_from_start() > 0) && (get_memory_cell(0) > 0))) {
		if (get_ran(1,1,100) < 40) 
			return_to_start(ME,1);
		}
		else if (get_memory_cell(0) == 0) {
			fidget(ME,25);
			}

	// if we're in combat and the above didn't give me anything to do, just
	// stop now. Otherwise, game will keep running script, and that eats up CPU time.
	if (am_i_doing_action() == FALSE)
		end_combat_turn();
break;

beginstate 3; // attacking

	if (target_ok() == FALSE)
		set_state(START_STATE);
	else
		do_attack();
	
break;

beginstate TALKING_STATE;
	if (get_memory_cell(3) == 0) {
		print_str("Talking: It doesn't respond.");
		end();
		}
	begin_talk_mode(get_memory_cell(3));
break;